module MappingTest
  
# You must have a setup function defined for these variables
#  def setup
#    @mapped_1 = nil      # valid instance of first mapped part, eg, users(:valid_user).id
#    @mapped_2 = nil      # valid instance of second mapped part, eg, jobs(:valid_job).id
#    @mapping_class = nil # mapping classname, eg, Userskill
#    @mapped_attr_1 = nil # symbol, eg, :user_id
#    @mapped_attr_2 = nil
#    @bad_mapping_value = 2342387957829347
#  end

  def test_exists
    assert !@mapping_class.exists?(@mapped_1, @mapped_2)
  end

  def test_should_add_valid_mapping
    mapping = create_mapping(@mapped_attr_1 => @mapped_1, @mapped_attr_2 => @mapped_2)
    assert mapping.valid?, "Should be valid mapping with #{@mapped_1}, #{@mapped_2}"
  end

  def test_should_not_add_mapping_with_nil_ids
    mapping = create_mapping(@mapped_attr_1 => nil)
    assert mapping.errors.on(@mapped_attr_1)
    mapping = create_mapping(@mapped_attr_2 => nil)
    assert mapping.errors.on(@mapped_attr_2)
  end

  def test_should_not_add_mapping_with_bad_id
    mapping = create_mapping(@mapped_attr_1 => @bad_mapping_value)
    assert mapping.errors.on(@mapped_attr_1)
    
    mapping = create_mapping(@mapped_attr_2 => @bad_mapping_value)
    assert mapping.errors.on(@mapped_attr_2)
  end

  def test_should_not_add_duplicate_mapping
    mapping = create_mapping
    assert mapping.valid?, "Should be valid mapping with #{@mapped_1}, #{@mapped_2}"
    mapping.save!
    mapping2 = create_mapping
    assert mapping2.errors.on_base
    assert !mapping2.valid?
  end
  
  protected
    # Try to create a mapping (with a set of valid options which can be overridden)
    def create_mapping(options = {})
      @mapping_class.create( { @mapped_attr_1 => @mapped_1, @mapped_attr_2 => @mapped_2 }.merge(options) )
    end
 
end
